﻿Imports System.Data.OleDb, System.Drawing
Public Class addResult
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_SubmitResult_Click(sender As Object, e As EventArgs) Handles btn_SubmitResult.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("soccerConnectionString").ConnectionString)
        Dim SqlString As String = "Insert Into fixture(divisionId, homeTeamId, awayTeamId , homeTeamScore,awayTeamScore, fixtureDate, fixturePhoto, venueId, reportedBy) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8,@f9)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim myGUID, myGUID2 As New System.Guid
        myGUID = Guid.NewGuid()
        myGUID2 = Guid.NewGuid()
        Dim newFileName = myGUID.ToString() + ".jpg"
        Dim newFileName2 = myGUID2.ToString() + ".jpg"

        If F_Picture.HasFile Then
            F_Picture.SaveAs(Request.MapPath("pictures") & "/" & newFileName)
            Using OriginalImage = Image.FromFile(Request.MapPath("pictures") & "/" & newFileName)
                Using ResizedImage As New Bitmap(OriginalImage, 500, 500)
                    ResizedImage.Save(Request.MapPath("pictures") & "/" & newFileName2, Drawing.Imaging.ImageFormat.Jpeg)

                End Using
            End Using
        End If
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", dd_Division.SelectedValue)
        cmd.Parameters.AddWithValue("@f2", dd_HomeTeam.SelectedValue)
        cmd.Parameters.AddWithValue("@f3", dd_AwayTeam.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", tb_HomeTeamScore.Text)
        cmd.Parameters.AddWithValue("@f5", tb_AwayTeamScore.Text)
        cmd.Parameters.AddWithValue("@f6", tb_FixtureDate.Text)
        cmd.Parameters.AddWithValue("@f7", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f8", dd_venueId.Text)
        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("resultconfirm.aspx")
    End Sub

    Protected Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs) Handles Calendar1.SelectionChanged
        tb_FixtureDate.Text = DateTime.Parse(Calendar1.SelectedDate)
    End Sub
End Class